package com.syxp.pda.service.plan.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.transaction.annotation.Transactional;

import com.syxp.pda.core.dao.BaseDao;
import com.syxp.pda.core.service.impl.BaseServiceImpl;
import com.syxp.pda.core.util.ArraysBuilder;
import com.syxp.pda.dao.base.DepartmentDao;
import com.syxp.pda.dao.base.DutyDao;
import com.syxp.pda.dao.base.UserDao;
import com.syxp.pda.entity.base.CHKLine;
import com.syxp.pda.entity.base.Duty;
import com.syxp.pda.entity.base.User;
import com.syxp.pda.service.plan.CHKLineConfService;

/**
 * 线路配置
 * @version 1.0 2011/07/25
 * @author Light
 *
 */
@Transactional
public class CHKLineConfServiceImpl extends BaseServiceImpl<CHKLine> implements CHKLineConfService {
	
	private DepartmentDao departmentDao;
	private DutyDao dutyDao;
	private UserDao userDao;

	public CHKLineConfServiceImpl(BaseDao<CHKLine> baseDao) {
		super(baseDao);
	}

	@Override
	public List<Duty> getDutys(int departID) {
		List<Duty> list = new ArrayList<Duty>();
		list.addAll(this.departmentDao.get(departID).getDutys());
		return list;
	}

	@Override
	public List<User> getUsers(int dutyID) {
		List<User> list = new ArrayList<User>();
		Set<User> users = this.dutyDao.get(dutyID).getUsers();
		for(User u: users) {
			u.setChkLines(ArraysBuilder.buildSet(u.getChkLines()));
			list.add(u);
		}
		return list;
	}

	@Override
	public List<User> getLineUser(int lineID) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * @return the departmentDao
	 */
	public DepartmentDao getDepartmentDao() {
		return departmentDao;
	}

	/**
	 * @param departmentDao the departmentDao to set
	 */
	public void setDepartmentDao(DepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}

	/**
	 * @return the dutyDao
	 */
	public DutyDao getDutyDao() {
		return dutyDao;
	}

	/**
	 * @param dutyDao the dutyDao to set
	 */
	public void setDutyDao(DutyDao dutyDao) {
		this.dutyDao = dutyDao;
	}

	@Override
	public void updateLine(int lineID, String ids) {
		if(ids==null||"".equals(ids.trim())) return ;
		CHKLine line = this.loadService(lineID);
		Set<User> users = line.getUsers();
		users.clear();
		this.updateService(line);
		String[] tmp = ids.split(",");
		for(String id: tmp) {
			users.add(this.userDao.get(Integer.parseInt(id)));
		}
		this.updateService(line);
	}

	/**
	 * @return the userDao
	 */
	public UserDao getUserDao() {
		return userDao;
	}

	/**
	 * @param userDao the userDao to set
	 */
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}


}
